//
//  ZhangqiRatingView.m
//  MovieProject
//
//  Created by 张琪( ⊙ o ⊙ )！ on 13-8-30.
//  Copyright (c) 2013年 张琪( ⊙ o ⊙ )！. All rights reserved.
//

#import "ZhangqiRatingView.h"
#define starWidth 35
#define starHeight 33
@implementation ZhangqiRatingView

- (id)initWithFrame:(CGRect)frame labelTextColor:(UIColor *)labelTextColor
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //计算缩放比例
        _scale = self.height * .6 /starHeight;
        
        //1.初始化灰色星星的视图
        UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, starWidth * 5, starHeight)];
        //设置背景
        grayView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"gray@2x.png"]];
        //设置缩放比例
        grayView.transform = CGAffineTransformMakeScale(_scale, _scale);
        //重新设定FRAME
        grayView.origin = CGPointMake(0, self.height * .2);
        [self addSubview:grayView];
        
        
        //2.初始化黄色星星的视图
        UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, starWidth * 5, starHeight)];
        yellowView.tag = 110;
        //设置背景
        yellowView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yellow@2x.png"]];
        //设置缩放比例
        yellowView.transform = CGAffineTransformMakeScale(_scale, _scale);
        //重新设定FRAME
        yellowView.origin = CGPointMake(0, self.height * .2);
        [self addSubview:yellowView];
        
        //3.设置评分的Label
        //----------------------创建Label------------------
        //个位Label
        UILabel *unitLabel = [[UILabel alloc]initWithFrame:CGRectMake(grayView.right + 10 * _scale, self.height * .25, self.height * .4, self.height * .75)];
        unitLabel.tag = 101;
        unitLabel.textColor = labelTextColor;
        unitLabel.font = [UIFont fontWithName:@"Bodoni 72" size:self.height * .75];
        unitLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:unitLabel];
        
        //点Label
        UILabel *dotLabel = [[UILabel alloc]initWithFrame:CGRectMake(unitLabel.right, 0, self.height * .3,self.height)];
        dotLabel.tag = 102;
        dotLabel.textColor = labelTextColor;
        dotLabel.font = [UIFont boldSystemFontOfSize:self.height];
        dotLabel.backgroundColor = [UIColor clearColor];
        dotLabel.text = [NSString stringWithFormat:@"·"];
        [self addSubview:dotLabel];
        
        //小数Label
        UILabel *decimalLabel = [[UILabel alloc]initWithFrame:CGRectMake(dotLabel.right, 0, self.height * .5, self.height * .75)];
        decimalLabel.tag = 103;
        decimalLabel.textColor = labelTextColor;
        decimalLabel.font = [UIFont fontWithName:@"Bodoni 72" size:self.height * .75];//Party LET-Bodoni 72 Oldstyle
        decimalLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:decimalLabel];
        
        //设置自身的宽度
        self.width = decimalLabel.right;
        self.backgroundColor = [UIColor clearColor];
        
        
    }
    return self;
}

- (void)setScoreNum:(float)scoreNum
{
    _scoreNum = scoreNum;
    
    //设置黄色星星的宽度
    UIView *yellowView = [self viewWithTag:110];
    yellowView.width = (starWidth * 5 * _scale) * (_scoreNum / 10);
    
    //格式化分数
    NSString *scoreSting = [NSString stringWithFormat:@"%.1f",_scoreNum];
    NSArray *score = [scoreSting componentsSeparatedByString:@"."];
    
    //个位Label
    UILabel *unitLabel = (UILabel *)[self viewWithTag:101];
    unitLabel.text = score[0];
    
    //小数Label
    UILabel *decimalLabel = (UILabel *)[self viewWithTag:103];
    decimalLabel.text = score[1];
}


@end
